import superService from "./superService";
import gameConfig from "../configs/gameConfig";
import miniGameService from "./miniGameService";
import { DEEP_NEXT_SPLIT } from "../configs/logConfig";
import O1HD from "../gameEngine";

export default class ossService extends superService {

    protected className: string = 'oss服务';

    //地址
    private readonly m_httpUrl: string = 'https://cdn.tobetopone.com/oss/twsjwzb/#.json';
    private readonly m_ossVersion: string = '1.0.0';
    //
    private m_platform: string = 'wx';

    constructor() {
        super();
    }

    getVersion() {
        return this.m_ossVersion;
    }

    getPlatform(): string {
        return this.m_platform;
    }

    //
    showOssPathMiniGameJumpIcon(iconId: string, sprite: cc.Sprite) {
        let url = this.getPlatformJumpUrl(iconId);
        O1HD.getServices().miniGameSrv.getAppApiInstance().drawUrlImage(url, spf => {
            sprite.spriteFrame = spf;
        });
    }

    private getPlatformJumpUrl(iconId: string) {
        let path = this.m_httpUrl.replace('.json', '/');
        path = path.replace('#', this.m_platform);
        path += `${iconId}.jpg`;
        console.log('跳转小游戏图标 最终路径', path);
        return path;
    }

    async loadService(): Promise<void> {
        await super.loadService();
        this.onSetPlatform();
        //return this.loadConfig();
    }

    private getPlatformUrl() {
        let fileName = this.m_platform + '-' + this.m_ossVersion;

        let timer = `?time=${Math.floor(Date.now() / (1000 * 60 * 5))}`;
        //
        if (!miniGameService.getMiniGameAppStatic().isProduction()) {
            const cdnTestTime = this.m_httpUrl.replace(/#/g, fileName + '-test') + timer;
            return cdnTestTime;
        }
        const cdnTime = this.m_httpUrl.replace(/#/g, fileName) + timer;
        return cdnTime;
    }

    private onSetPlatform() {
        switch (cc.sys.platform) {
            case cc.sys.WECHAT_GAME:
            case cc.sys.WECHAT_GAME_SUB: {
                this.m_platform = 'ks';
                break;
            }
            case cc.sys.BYTEDANCE_GAME:
            case cc.sys.BYTEDANCE_GAME_SUB: {
                this.m_platform = 'tt';
                break;
            }
            case cc.sys.HUAWEI_GAME: {
                console.log('华为快游戏');
                this.m_platform = 'huawei';
                break;
            }
            case cc.sys.VIVO_GAME: {
                console.log('vivo快游戏');
                this.m_platform = 'vivo';
                break;
            }
            case cc.sys.OPPO_GAME: {
                console.log('oppo快游戏');
                this.m_platform = 'oppo';
                break;
            }
            default: {
                if (cc.sys.isNative) {
                    if (cc.sys.os == cc.sys.OS_ANDROID) {
                        console.log('Android 游戏');
                        this.m_platform = 'app/Android';
                    } else if (cc.sys.os == cc.sys.OS_IOS) {
                        console.log('iOS 游戏');
                        this.m_platform = 'app/iOS';
                    } else {
                        this.m_platform = 'tt';
                    }
                } else {
                    if (cc.sys.platform == 3 || cc.sys.platform == 4) {
                        console.log('快手小游戏');
                        this.m_platform = 'ks';
                    } else {
                        console.log('h5小游戏');
                        this.m_platform = 'h5';
                    }
                }
                break;
            }
        }
        // 测试
        // this.m_platform = 'app/Android';
    }

    private async loadConfig(): Promise<void> {
        //
        return new Promise<void>(resolve => {
            //
            const url = this.getPlatformUrl();

            cc.assetManager.loadRemote(url, (err, textAsset: cc.JsonAsset) => {
                if (err) {
                    console.error(DEEP_NEXT_SPLIT, '#20错误的加载', err);
                    setTimeout(() => {
                        cc.game.restart();
                    }, 1500);
                    return;
                }

                const json = textAsset.json;
                console.log(DEEP_NEXT_SPLIT, 'oss 加载到的数值', json);

                //修改配置
                let keys = Object.keys(gameConfig);
                for (let key in json) {
                    if (keys.includes(key)) {
                        // console.log(DEEP_NEXT_SPLIT, 'oss 重写', key, json[key]);
                        gameConfig[key] = json[key];
                    }
                }

                resolve();
            });
        });
    }
}



































